import api from '../../api';
import { message } from 'antd';

export default {
  namespace: 'ratingmonitoringM',
  state: {
    comcodeList: [], //主体名称列表
    columnsInfo: {
      // 列选择 表头
      0: { columns: [], checkedKeys: [], treeData: [] },
      1: { columns: [], checkedKeys: [], treeData: [] },
      2: { columns: [], checkedKeys: [], treeData: [] },
    },
    checkedKeysInfo: {
      // 列选择 选中项
      0: { checkedKeys: [] },
      1: { checkedKeys: [] },
      2: { checkedKeys: [] },
    },
  },
  effects: {
    //查询主体名称模糊查询
    *queryComcode({ payload, callback }, { call, put }) {
      const response = yield call(api.ratingmonitoring.queryComcode, payload);
      if (response) {
        if (response.code == 0) {
          callback(payload.name, response.data.dataSource);
        } else {
          yield put({
            type: 'setState',
            payload: {
              comcodeList: [],
            },
          });
          message.error(response.msg || '查询主体名称下拉列表失败!');
        }
      } else {
        yield put({
          type: 'setState',
          payload: {
            comcodeList: [],
          },
        });
        message.error('查询主体名称下拉列表失败!');
      }
    },
    //债券代码模糊查询
    *queryBondId({ payload, callback }, { call, put }) {
      const response = yield call(api.ratingmonitoring.queryBondId, payload);
      callback(response);
    },

    //查询主体评级监控
    *queryMainList({ payload, callback }, { call, put }) {
      const response = yield call(api.ratingmonitoring.queryMainList, payload);
      if (response) {
        if (response.code == 0) {
          callback('success', response.data);
        } else {
          callback('fail');
          message.error(response.msg || '查询主体评级监控失败!');
        }
      } else {
        callback('fail');
        message.error('查询主体评级监控失败!');
      }
    },
    //获取债券评级监控列表
    *queryBondRatingList({ payload, callback }, { call, put }) {
      const response = yield call(api.ratingmonitoring.queryBondRatingList, payload);
      if (response) {
        if (response.code == 0) {
          callback('success', response.data);
        } else {
          callback('fail');
          message.error(response.msg || '获取债券评级监控列表失败!');
        }
      } else {
        callback('fail');
        message.error('获取债券评级监控列表失败!');
      }
    },
    //获取中债隐含评级监控列表
    *queryList({ payload, callback }, { call, put }) {
      const response = yield call(api.ratingmonitoring.queryList, payload);
      if (response) {
        if (response.code == 0) {
          callback('success', response.data);
        } else {
          callback('fail');
          message.error(response.msg || '获取中债隐含评级监控列表失败!');
        }
      } else {
        callback('fail');
        message.error('获取中债隐含评级监控列表失败!');
      }
    },
    //获取监控历史
    *queryHistory({ payload, callback }, { call, put }) {
      const response = yield call(api.ratingmonitoring[payload.url], payload);
      if (response) {
        const resultMessage = response.resultMessage || {};
        if (resultMessage.code == 0) {
          callback('success', response.resultMessage.data, response.basicInfo);
        } else {
          callback('fail');
          message.error(response.msg || '获取历史主体评级信息失败!');
        }
      } else {
        callback('fail');
        message.error('获取历史主体评级信息失败!');
      }
    },
    //获取监控持仓
    *queryHold({ payload, callback }, { call, put }) {
      const response = yield call(api.ratingmonitoring[payload.url], payload);
      if (response) {
        if (response.code == 0) {
          callback('success', response.data);
        } else {
          callback('fail');
          message.error(response.msg || '获取关联持仓列表失败!');
        }
      } else {
        callback('fail');
        message.error('获取关联持仓列表失败!');
      }
    },
    //查询获取表头信息
    *queryShowCol({ payload, callback }, { call, put, select }) {
      let type = payload.tab;
      delete payload.tab;
      let res = yield call(api.positionInquiry.queryShowCol, payload);
      if (res && res.code == 0 && res.data.dataSource) {
        const columns = res.data.dataSource.columns;
        const checkedKeys = [];
        const treeData = [
          {
            title: '全部',
            key: 'all',
            children: res.data.dataSource.columns,
          },
        ];
        //得到选中项
        res.data.dataSource.columns.map((item) => {
          if (item.display == true) {
            checkedKeys.push(item.key);
          }
        });

        if (type == 0) {
          let columnsInfo = yield select((state) => state.ratingmonitoringM.columnsInfo);
          let checkedKeysInfo = yield select((state) => state.ratingmonitoringM.checkedKeysInfo);
          columnsInfo = {
            ...columnsInfo,
            0: { columns: columns, checkedKeys: checkedKeys, treeData: treeData },
          };
          checkedKeysInfo = {
            ...checkedKeysInfo,
            0: { checkedKeys: checkedKeys },
          };
          yield put({
            type: 'setState',
            payload: {
              columnsInfo,
              checkedKeysInfo,
            },
          });
        }

        if (type == 1) {
          let columnsInfo = yield select((state) => state.ratingmonitoringM.columnsInfo);
          let checkedKeysInfo = yield select((state) => state.ratingmonitoringM.checkedKeysInfo);
          columnsInfo = {
            ...columnsInfo,
            1: { columns: columns, checkedKeys: checkedKeys, treeData: treeData },
          };
          checkedKeysInfo = {
            ...checkedKeysInfo,
            1: { checkedKeys: checkedKeys },
          };

          yield put({
            type: 'setState',
            payload: {
              columnsInfo,
              checkedKeysInfo,
            },
          });
        }

        if (type == 2) {
          let columnsInfo = yield select((state) => state.ratingmonitoringM.columnsInfo);
          let checkedKeysInfo = yield select((state) => state.ratingmonitoringM.checkedKeysInfo);
          columnsInfo = {
            ...columnsInfo,
            2: { columns: _.cloneDeep(columns), checkedKeys: _.cloneDeep(checkedKeys), treeData: _.cloneDeep(treeData) },
          };
          checkedKeysInfo = {
            ...checkedKeysInfo,
            2: { checkedKeys: _.cloneDeep(checkedKeys) },
          };
          yield put({
            type: 'setState',
            payload: {
              columnsInfo,
              checkedKeysInfo,
            },
          });
        }
      } else {
        message.error(res?.message || '查询获取表头信息失败');
      }
    },

    //保存表头信息
    *saveCol({ payload }, { call, put, select }) {
      const res = yield call(api.positionInquiry.saveCol, payload);
      return res;
    },
  },

  reducers: {
    setState(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
